package redis

import (
	"context"
	"crypto/tls"
	"fmt"
	"gitee.com/baizhige/go-server-tempalte/common/config"
	"github.com/redis/go-redis/v9"
	"os"
)

var cluster redis.Cmdable

func Init(conf config.RedisConfig) {
	// redis
	opt := &redis.Options{
		Addr:     conf.Addr,
		DB:       conf.Db,
		Password: conf.Password,
	}
	if conf.TLS {
		opt.TLSConfig = &tls.Config{}
	}
	cluster = redis.NewClient(opt)

	err := cluster.Ping(context.Background()).Err()
	if err != nil {
		fmt.Printf("error to connect redis: %s\n", err)
		os.Exit(1)
	} else {
		fmt.Printf("connect to redis success: %s\n", conf.Addr)
	}
}
